package com.ruoyi.pos.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pos.domain.payment.PosPaymentType;
import com.ruoyi.pos.domain.dto.payment.request.PaymentTypeQueryRequest;
import com.ruoyi.pos.domain.dto.payment.response.PaymentTypeResponse;
import com.ruoyi.pos.domain.dto.common.response.BasePageResponse;
import com.ruoyi.pos.mapper.payment.PosPaymentTypeMapper;
import com.ruoyi.pos.service.payment.IPosPaymentTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * POS支付方式管理Controller
 * 
 * @author ruoyi
 * @date 2025-01-30
 */
@Api(tags = "POS支付方式管理")
@RestController
@RequestMapping("/pos/payment")
public class PosPaymentTypeController extends BaseController {

    @Autowired
    private IPosPaymentTypeService posPaymentTypeService;
    
    @Autowired
    private PosPaymentTypeMapper posPaymentTypeMapper;

    /**
     * 查询支付方式列表
     */
    @ApiOperation("查询支付方式列表")
    @PreAuthorize("@ss.hasPermi('pos:payment:list')")
    @GetMapping("/list")
    public TableDataInfo list(PaymentTypeQueryRequest request) {
        startPage();
        BasePageResponse<PaymentTypeResponse> response = posPaymentTypeService.queryPaymentTypes(request);
        TableDataInfo dataTable = new TableDataInfo();
        dataTable.setCode(response.getCode());
        dataTable.setMsg(response.getMsg());
        dataTable.setRows(response.getRows());
        dataTable.setTotal(response.getTotal());
        return dataTable;
    }

    /**
     * 导出支付方式列表
     */
    @ApiOperation("导出支付方式列表")
    @PreAuthorize("@ss.hasPermi('pos:payment:export')")
    @Log(title = "POS支付方式", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PosPaymentType posPaymentType) {
        List<PosPaymentType> list = posPaymentTypeMapper.selectPosPaymentTypeList(posPaymentType);
        ExcelUtil<PosPaymentType> util = new ExcelUtil<PosPaymentType>(PosPaymentType.class);
        util.exportExcel(response, list, "支付方式数据");
    }

    /**
     * 获取支付方式详细信息
     */
    @ApiOperation("获取支付方式详细信息")
    @PreAuthorize("@ss.hasPermi('pos:payment:query')")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@ApiParam("支付方式ID") @PathVariable Long typeId) {
        PaymentTypeQueryRequest request = new PaymentTypeQueryRequest();
        request.setTypeId(typeId);
        return success(posPaymentTypeService.getPaymentType(request));
    }

    /**
     * 根据编码获取支付方式
     */
    @ApiOperation("根据编码获取支付方式")
    @PreAuthorize("@ss.hasPermi('pos:payment:query')")
    @GetMapping(value = "/code/{typeCode}")
    public AjaxResult getByCode(@ApiParam("支付方式编码") @PathVariable String typeCode) {
        return success(posPaymentTypeMapper.selectPosPaymentTypeByCode(typeCode));
    }

    /**
     * 新增支付方式
     */
    @ApiOperation("新增支付方式")
    @PreAuthorize("@ss.hasPermi('pos:payment:add')")
    @Log(title = "POS支付方式", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PosPaymentType posPaymentType) {
        return toAjax(posPaymentTypeMapper.insertPosPaymentType(posPaymentType));
    }

    /**
     * 修改支付方式
     */
    @ApiOperation("修改支付方式")
    @PreAuthorize("@ss.hasPermi('pos:payment:edit')")
    @Log(title = "POS支付方式", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PosPaymentType posPaymentType) {
        return toAjax(posPaymentTypeMapper.updatePosPaymentType(posPaymentType));
    }

    /**
     * 删除支付方式
     */
    @ApiOperation("删除支付方式")
    @PreAuthorize("@ss.hasPermi('pos:payment:remove')")
    @Log(title = "POS支付方式", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeIds}")
    public AjaxResult remove(@ApiParam("支付方式ID数组") @PathVariable Long[] typeIds) {
        return toAjax(posPaymentTypeMapper.deletePosPaymentTypeByIds(typeIds));
    }

    /**
     * 修改支付方式状态
     */
    @ApiOperation("修改支付方式状态")
    @PreAuthorize("@ss.hasPermi('pos:payment:edit')")
    @Log(title = "修改支付方式状态", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody PosPaymentType posPaymentType) {
        return toAjax(posPaymentTypeMapper.updatePosPaymentType(posPaymentType));
    }

    /**
     * 获取可用支付方式列表（供POS端使用）
     */
    @ApiOperation("获取可用支付方式列表")
    @GetMapping("/available")
    public AjaxResult getAvailable() {
        PosPaymentType query = new PosPaymentType();
        query.setIsEnabled("1"); // 1-启用
        List<PosPaymentType> list = posPaymentTypeMapper.selectPosPaymentTypeList(query);
        return success(list);
    }
}

